package com.yidu.labyrinth.controller;

import com.yidu.labyrinth.main.*;
import com.yidu.labyrinth.service.LabyrinthRoleService;
import com.yidu.labyrinth.service.LabyrinthRolesModulesService;
import com.yidu.labyrinth.utils.BaseMessage;
import com.yidu.labyrinth.utils.State;
import com.yidu.labyrinth.utils.Tools;
import com.yidu.labyrinth.vo.LabyrinthVo;
import com.yidu.labyrinth.vo.Pages;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;

import java.util.*;

/**
 * @program: labyrinth
 * @description 角色控制器
 * @Date:2019-09-25 13:46
 * @Author:谭俊
 **/
@Controller
@RequestMapping("/role")
public class LabyrinthRoleController {


    @Resource
    LabyrinthRoleService labyrinthRoleService;


    @Resource
    LabyrinthRolesModulesService labyrinthRolesModulesService;

    /**
     * 分页查询所有
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String,Object> findAll(LabyrinthVo labyrinthVo, Pages pages){

         //设置行数
        pages.setOverRows(labyrinthRoleService.count(labyrinthVo).intValue());

        Map<String,Object> map = new HashMap<>();

        List<LabyrinthVo> list = labyrinthRoleService.findAll(labyrinthVo,pages);

        System.out.println(pages.getOverRows()+"总");


        map.put("data",list);

        map.put("count",pages.getOverRows());

        map.put("code", 0);

        map.put("msg", "");//消息


        return map;

    }


    /**
     * 修改是 否有效
     * @return
     */
    @RequestMapping("result")
    @ResponseBody
    public int result(LabyrinthVo labyrinthVo){

        System.out.println(labyrinthVo.getRoleResult()+"汇总");
        return labyrinthRoleService.save(labyrinthVo);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @RequestMapping("findById")
    @ResponseBody
    public LabyrinthVo findById(String id){


        return labyrinthRoleService.findById(id);
    }


    /**
     * 查询模块
     * @return
     */
    @RequestMapping("board")
    @ResponseBody
    public List<JsonM> selectAll(){

        LabyrinthModuleEntity labyrinthModuleEntity = new LabyrinthModuleEntity();


        List<JsonM> list = labyrinthRoleService.board(labyrinthModuleEntity);

        System.out.println(list.size()+"长度");

        for (Iterator<JsonM> iterator = list.iterator(); iterator.hasNext(); ) {
            JsonM next =  iterator.next();
            System.out.println(next.getTitle()+"title"+next.getChildren()+next.getId());

        }

        return list;

    }

    /**
     * 增加修改
     * @param json
     * @param labyrinthRoleEntity
     * @return
     */
    @RequestMapping("addOrUpdate")
    @ResponseBody

    public BaseMessage addOrUpdate(String json, LabyrinthRoleEntity labyrinthRoleEntity){
        BaseMessage baseMessage = new BaseMessage();
/*
        BaseMessage baseMessage = new BaseMessage();

        Gson gson = new Gson();

        List<JsonM> list =gson.fromJson(json, new TypeToken<List<JsonM>>() {}.getType());

        String ids = "";

        for (Iterator<JsonM> iterator = list.iterator(); iterator.hasNext(); ) {

            JsonM jsonM =  iterator.next();

            List<JsonM> jsonMS = jsonM.getChildren();


            for (Iterator<JsonM> jsonMIterator = jsonMS.iterator(); jsonMIterator.hasNext(); ) {
                JsonM next =  jsonMIterator.next();

                ids+=next.getId()+"&";


            }

        }

        if(StringUtils.isEmpty(labyrinthRoleEntity.getRoleId())){


            labyrinthRoleService.add(labyrinthRoleEntity);

        }else{

            labyrinthRoleService.update(labyrinthRoleEntity);
        }



        if(ids.length()>0){

            String[] idss = ids.split("&");

            //先把关于这个角色的模板权限清空

            LabyrinthModuleOrRoleEntity labyrinthModuleOrRoleEntity = new LabyrinthModuleOrRoleEntity();

            labyrinthModuleOrRoleEntity.setRoleId(labyrinthRoleEntity.getRoleId());

            //labyrinthModuleOrRoleEntity.setModuleId(labyrinthRoleEntity);

            System.out.println(labyrinthRoleEntity.getRoleId()+"角色id"+labyrinthModuleOrRoleEntity.getModuleId()+labyrinthModuleOrRoleEntity.getModuleOrRoleId());

           labyrinthRolesModulesService.deleteAll(labyrinthModuleOrRoleEntity);

            //循环id数组
            for (int i = 0; i < idss.length; i++) {
                // new出关联对象
                LabyrinthModuleOrRoleEntity labyrinthModuleOrRoleEntity2 = new LabyrinthModuleOrRoleEntity();

                //放入角色id

                labyrinthModuleOrRoleEntity2.setRoleId(labyrinthRoleEntity.getRoleId());

                //放入模板id
                labyrinthModuleOrRoleEntity2.setModuleOrRoleId(labyrinthModuleOrRoleEntity2.getModuleId());
                System.out.println(labyrinthModuleOrRoleEntity2.getModuleId()+"22222");

                labyrinthRolesModulesService.add(labyrinthModuleOrRoleEntity2);


            }
        }

        baseMessage.setState(1);
        baseMessage.setMsg("编辑成功");*/

        return  baseMessage;


    }



    @RequestMapping("select")
    @ResponseBody
    public List<LabyrinthModuleOrRoleEntity> select(LabyrinthModuleOrRoleEntity labyrinthModuleOrRoleEntity){

        return null;
    }


    /*
     * 查询所有模板，树形组件
     */
    @RequestMapping("findByAllTwo")
    @ResponseBody
    public List<NodeRole> findByAllTwo(){

        System.out.println("进方法");


        List<LabyrinthVo> list = labyrinthRoleService.findAlls();

        List<NodeRole> listNode = new ArrayList<NodeRole>();
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            LabyrinthVo moduleVo = (LabyrinthVo) iterator.next();
            System.out.println(moduleVo.getModuleId());
            //新建一个node工具类对象
            NodeRole node = new NodeRole();
            //给工具类的id赋值
            node.setId(moduleVo.getModuleId());
            //给工具类的外键赋值
            node.setPid(moduleVo.getModuleIds());
            //给工具类的名称赋值
            node.setTitle(moduleVo.getModuleName());
            System.out.println(node.getTitle()+"我是谁");
            //最后将其加入到集合中
            listNode.add(node);
        }

        //新建一个node工具类对象
        NodeRole node = new NodeRole();
        //新建一个集合对象，将上一个集合加入到本集合中
        List<NodeRole> nodeList2 = node.createTree(listNode);
        System.out.println(nodeList2.get(0).getTitle()+"你是谁");

        return nodeList2;

    }


    /*
     * 查询所有模板，树形组件 ，是否选中
     */
    @RequestMapping("findByAllThree")
    @ResponseBody
    public List<NodeRole> findByAllThree(String id){

        List<LabyrinthVo> list = labyrinthRolesModulesService.findAll();
        System.out.println(list.size()+"集合为");
        List<NodeRole> listNode = new ArrayList<NodeRole>();
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            LabyrinthVo moduleVo = (LabyrinthVo) iterator.next();
                //新建一个node工具类对象
                NodeRole node = new NodeRole();
                //给工具类的id赋值
                node.setId(moduleVo.getModuleId());
                //给工具类的外键赋值
                node.setPid(moduleVo.getModuleIds());
                //给工具类的名称赋值
                node.setTitle(moduleVo.getModuleName());
                List<RoleModuleVo> lists = labyrinthRolesModulesService.findRoleId(id);
                for (int i = 0; i < lists.size(); i++) {
                    if(!lists.get(i).getModModuleId().equals("0")) {
                        if(lists.get(i).getModuleId().equals(moduleVo.getModuleId())) {
                            node.setChecked(true);
                            break ;
                        }else {
                            node.setChecked(false);
                        }
                    }
                }

                //最后将其加入到集合中
                listNode.add(node);
        }

        //新建一个node工具类对象
        NodeRole node = new NodeRole();
        //新建一个集合对象，将上一个集合加入到本集合中
        List<NodeRole> nodeList2 = node.createTree(listNode);
        System.out.println(nodeList2+"返还参数");

        return nodeList2;

    }




    /**
     * @param rolevo
     * @return
     */
    @RequestMapping("RoleInsert")
    @ResponseBody
    public State RoleInsert(LabyrinthVo rolevo) {
        State bm=new State();
        System.out.println(rolevo.getRoleId()+"角色id为");
        rolevo.setRoleCreateDate(Tools.getCurDateTime());
        if(rolevo.getRoleId()!=null && !"".equals(rolevo.getRoleId())) {

            labyrinthRolesModulesService.update(rolevo);
        }else {

            labyrinthRolesModulesService.add(rolevo);
        }

        bm.setState(1);
        bm.setMsg("操作成功");
        return bm;
    }



    @RequestMapping("RoleById")
    @ResponseBody
    public LabyrinthVo RoleById(String id) {

        LabyrinthVo rl= labyrinthRolesModulesService.findId(id);

        return rl;
    }








}
